#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <queue>

using std::vector;
using std::cin;
using std::cout;


void Input(int &n, int &m, vector<vector<int> > &matrix, int &x_start, int &y_start, int &x_finish, int &y_finish) {
    cin >> n >> m;
    matrix.resize(n);
    cin >> x_start >> y_start >> x_finish >> y_finish;
    x_start--;
    y_start--;
    x_finish--;
    y_finish--;
    for (int i = 0; i < n; ++i) {
        matrix[i].resize(m, 0);
        for (int j = 0; j < m; ++j) {
            cin >> matrix[i][j];
        }
    }
}

void CreateMarks(vector<vector<int> > &marks, int n, int m) {
    marks.resize(n);
    for (int i = 0; i < n; ++i) {
        marks[i].resize(m, -1);
    }
}

bool check(int x, int y, int n, int m) {
    if (x < 0 || y < 0) {
        return 0;
    }
    if (x >= n || y >= m) {
        return 0;
    }
    return 1;
}

void bfs(vector<vector<int> > &matrix, vector<vector<int> > &marks, int x_st, int y_st) {
    struct inque {
        int x, y, len;
        inque(int _x = 0, int _y = 0, int _len = 0) {
            x = _x;
            y = _y;
            len = _len;
        }
    };
    std::queue<inque> q;
    q.push(inque(x_st, y_st, 0));
    int x, y, l;
    int shift[2];
    shift[0] = 1;
    shift[1] = -1;
    inque current;
    while (q.size()) {
        current = q.front();
        x = current.x;
        y = current.y;
        l = current.len;
        q.pop();
        if (!check(x, y, matrix.size(), matrix[0].size())) {
            continue;
        }
        if (marks[x][y] + 1 || matrix[x][y]) {
            continue;
        }
        marks[x][y] = l;
        for (int i = 0; i < 2; ++i) {
            q.push(inque(x + shift[i], y, l + 1));
            q.push(inque(x, y  + shift[i], l + 1));
        }
    }
}


int main() {
    int n, m, x_start, y_start, x_finish, y_finish;
    vector<vector<int> > matrix;
    Input(n, m, matrix, x_start, y_start, x_finish, y_finish);
    vector<vector<int> > marks;
    CreateMarks(marks, n, m);
    bfs(matrix, marks, x_start, y_start);
    if (marks[x_finish][y_finish] + 1) {
        cout << marks[x_finish][y_finish];
    }
    else {
        cout << "NO";
    }
    return 0;
}